﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace VU_Meter
{
    public partial class VuMeter : UserControl
    {
        private Color c1 = Color.Green;
        private Color c2 = Color.Yellow;
        private Color c3 = Color.Red;
        protected float percent = 0.0f;

        public float Value
        {
            get
            {
                return -percent + 100;
            }
            set
            {
                if (value < 0) value = 0;
                else if (value > 100) value = 100;
                percent = 100 - value;
                this.Invalidate();
            }
        }
        public VuMeter()
        {
            InitializeComponent();


        }

        public Color Color1
        {
            get { return c1; }
            set { c1 = value; Invalidate(); }
        }

        public Color Color2
        {
            get { return c2; }
            set { c2 = value; Invalidate(); }
        }
        public Color Color3
        {
            get { return c3; }
            set { c3 = value; Invalidate(); }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Pen black = new Pen(Brushes.Black, 2);
            Color transColor = Color.FromArgb(200, Color.Black);
            ColorBlend blend = new ColorBlend(6);
            LinearGradientBrush gradientBrush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), c1, c2, LinearGradientMode.Vertical);
            SetGradients(blend, gradientBrush);

            int height = (int)((percent / 100) * this.Height);
            e.Graphics.FillRectangle(gradientBrush, 0, 0, this.Width, this.Height);
            e.Graphics.FillRectangle(new SolidBrush(transColor), 0, 0, this.Width, height);
            e.Graphics.DrawRectangle(black, 1, 1, this.Width - 6, this.Height - 6);
            for (int i = 1; i < 10; i++)
            {
                e.Graphics.DrawRectangle(black, 1, (i * this.Height / 10), this.Width - 6, 2);
            }
            black.Dispose();
            gradientBrush.Dispose();
        }

        public void SetGradients(ColorBlend blend, LinearGradientBrush gradientBrush)
        {
            blend.Colors[0] = c3;
            blend.Colors[1] = c3;
            blend.Colors[2] = c2;
            blend.Colors[3] = c2;
            blend.Colors[4] = c1;
            blend.Colors[5] = c1;
            blend.Positions[0] = 0.0f;
            blend.Positions[1] = 0.20f;
            blend.Positions[2] = 0.20f;
            blend.Positions[3] = 0.40f;
            blend.Positions[4] = 0.40f;
            blend.Positions[5] = 1.0f;
            gradientBrush.InterpolationColors = blend;
        }
    }
}
